function [TrainDS,ValDS,TestDS] = dataTVT(IMDS,ParamWN,opRand)
%dataTVS - Partition into training, validation, test datasets
%
%   [TrainDS,ValDS,TestDS] = dataTVT(IMDS,ParamWN,opRand)
%
% This function provides the datasets TrainDS, ValDS and TestDS for 
% training, validation and test on the basis of these input arguments:
%
% IMDS            : input dataset. If IMDS is undefined or empty, the name 
%                   of the file with such data can be interactively managed. 
%                   In this case, the file must contain a field whose name 
%                   is IMDS (case sensitive);
%
% ParamWN.VectPart: 3-elements vector such that: VectPart(1), VectPart(2) 
%                   and VectPart(3) are the fraction of images for training, 
%                   validation and test respectively.
%                   If ParamWN is a char variable, it is supposed to be 
%                   the name of a file having a field named 'ParamWN'. If 
%                   the file loading or the extraction of the ParamWN value 
%                   is not successful, it is ParamWN = [].
%                   If ParamWN is undefined or empty, a session of DefParam
%                   runs to define ParamWN.
%
% opRand          : if true, the partition is carried out in random way. If
%                   false, the partition is with the initial order.
%                   If opRand is undefined or empty, the default value (true)
%                   is used.
%  
% See also PluVelScalogram, trendClass, dataHomAug, TRLearnPluVel, DefParam.
%
%   [TrainDS,ValDS,TestDS] = dataTVT(IMDS,ParamWN,opRand)

% G. Teza, 2020

if nargin < 3 || isempty(opRand)
    opRand = 1;
end

if nargin < 2
    ParamWN = [];
end
if ischar(ParamWN)
    try 
        sv = load(ParamWN);
        ParamWN = sv.ParamWN;
    catch
        ParamWN = [];
    end
end
if isempty(ParamWN)
    ParamWN = DefParam;
end
vectPart = ParamWN.VectPart;

if nargin < 1 || isempty(IMDS)
    [filena,pathna] = uigetfile('.mat','Image Datastore file');
    netfile = fullfile(pathna,filena);
    IMDS = load(netfile);
    IMDS = IMDS.IMDS;
end

if opRand
    [TrainDS,ValDS,TestDS] = splitEachLabel(IMDS,...
        vectPart(1),vectPart(2),vectPart(3),...
        'randomized');
else
    [TrainDS,ValDS,TestDS] = splitEachLabel(IMDS,...
        vectPart(1),vectPart(2),vectPart(3));
end

tTr = TrainDS.countEachLabel;
tVa = ValDS.countEachLabel;
tTe = TestDS.countEachLabel;
fprintf('\nTraining dataset label count:\n\n');
disp(tTr);
fprintf('\nValidation dataset label count:\n\n');
disp(tVa);
fprintf('\nTest dataset label count:\n\n');
disp(tTe);